﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using ZK.HardWareSDK;

namespace ZK.HardWareBLL
{
    public class Onbon6E1XCardBLL_2
    {
        static OnbonBx_sdk_dualNewSDK.EQareaHeader_G6 aheader;
        static OnbonBx_sdk_dualNewSDK.EQSound_6G stSoundData = new OnbonBx_sdk_dualNewSDK.EQSound_6G();
        static OnbonBx_sdk_dualNewSDK.EQpageHeader_G6 pheader;
        static OnbonBx_sdk_dualNewSDK.DynamicAreaParams[] Params = new OnbonBx_sdk_dualNewSDK.DynamicAreaParams[1];
        static Models.TerminalM terminalM;
       
        public static int InitSdk(Models.TerminalM terminal)
        {
            terminalM = terminal;
            {
                aheader.AreaType = 0x00;
                aheader.AreaX = 0;
                aheader.AreaY = 0;
                aheader.AreaWidth = (ushort)terminal.width;
                aheader.AreaHeight = (ushort)terminal.height;
                aheader.BackGroundFlag = 0x00;
                aheader.Transparency = 101;
                aheader.AreaEqual = 0x00;
            }

            {
                stSoundData.SoundFlag = 0;
                stSoundData.SoundVolum = 0;
                stSoundData.SoundSpeed = 0;
                stSoundData.SoundDataMode = 0;
                stSoundData.SoundReplayTimes = 0;
                stSoundData.SoundReplayDelay = 0;
                stSoundData.SoundReservedParaLen = 0;
                stSoundData.Soundnumdeal = 0;
                stSoundData.Soundlanguages = 0;
                stSoundData.Soundwordstyle = 0;
                stSoundData.SoundDataLen = 0;
                byte[] t = new byte[1];
                t[0] = 0;
                stSoundData.SoundData = t;
            }

            {
                pheader.PageStyle = 0x00;
                pheader.DisplayMode = 0x02;
                pheader.ClearMode = 0x01;
                pheader.Speed = 15;
                pheader.StayTime = 500;
                pheader.RepeatTime = 1;
                pheader.ValidLen = 0;
                pheader.CartoonFrameRate = 0x00;
                pheader.BackNotValidFlag = 0x00;
                pheader.arrMode = OnbonBx_sdk_dualNewSDK.E_arrMode.eMULTILINE;
                pheader.fontSize = 9;
                pheader.color = (uint)0x01;
                pheader.fontBold = 1;
                pheader.fontItalic = 0;
                pheader.tdirection = OnbonBx_sdk_dualNewSDK.E_txtDirection.pNORMAL;
                pheader.txtSpace = 0;
                pheader.Valign = 2;
                pheader.Halign = 2;
            }



            return OnbonBx_sdk_dualNewSDK.bxDual_InitSdk();
        }

        /// <summary>
        /// 更新动态区
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="ipStr"></param>
        public static void sendMessage(string msg)
        {
            string ipStr = terminalM.ip;
            int err = 0;
            //Console.Write("输入IP:");
            //byte[] ip = Encoding.Default.GetBytes(Console.ReadLine());
            byte[] ip = Encoding.Default.GetBytes(ipStr);
            // byte[] img = Encoding.Default.GetBytes("32.png");
            byte[] str = Encoding.Default.GetBytes(msg);
            byte[] font = Encoding.Default.GetBytes("黑体");
            //OnbonBx_sdk_dualNewSDK.EQareaHeader_G6 aheader;
            //aheader.AreaType = 0x00;
            //aheader.AreaX = 0;
            //aheader.AreaY = 0;
            //aheader.AreaWidth = 96;
            //aheader.AreaHeight = 48;
            //aheader.BackGroundFlag = 0x00;
            //aheader.Transparency = 101;
            //aheader.AreaEqual = 0x00;
            //OnbonBx_sdk_dualNewSDK.EQareaHeader_G6 aheader1;
            //aheader1.AreaType = 0x10;
            //aheader1.AreaX = 48;
            //aheader1.AreaY = 0;
            //aheader1.AreaWidth = 32;
            //aheader1.AreaHeight = 32;
            //aheader1.BackGroundFlag = 0x00;
            //aheader1.Transparency = 101;
            //aheader1.AreaEqual = 0x00;
            // OnbonBx_sdk_dualNewSDK.EQSound_6G stSoundData = new OnbonBx_sdk_dualNewSDK.EQSound_6G();
            //stSoundData.SoundFlag = 0;
            //stSoundData.SoundVolum = 0;
            //stSoundData.SoundSpeed = 0;
            //stSoundData.SoundDataMode = 0;
            //stSoundData.SoundReplayTimes = 0;
            //stSoundData.SoundReplayDelay = 0;
            //stSoundData.SoundReservedParaLen = 0;
            //stSoundData.Soundnumdeal = 0;
            //stSoundData.Soundlanguages = 0;
            //stSoundData.Soundwordstyle = 0;
            //stSoundData.SoundDataLen = 0;
            //byte[] t = new byte[1];
            //t[0] = 0;
            //stSoundData.SoundData = t;
            aheader.stSoundData = stSoundData;
            //aheader1.stSoundData = stSoundData;

            //string str = "Hello,LED789";
            // OnbonBx_sdk_dualNewSDK.EQpageHeader_G6 pheader;
            //pheader.PageStyle = 0x00;
            //pheader.DisplayMode = 0x02;
            //pheader.ClearMode = 0x01;
            //pheader.Speed = 15;
            //pheader.StayTime = 500;
            //pheader.RepeatTime = 1;
            //pheader.ValidLen = 0;
            //pheader.CartoonFrameRate = 0x00;
            //pheader.BackNotValidFlag = 0x00;
            //pheader.arrMode = OnbonBx_sdk_dualNewSDK.E_arrMode.eMULTILINE;
            //pheader.fontSize = 12;
            //pheader.color = (uint)0x01;
            //pheader.fontBold = 1;
            //pheader.fontItalic = 0;
            //pheader.tdirection = OnbonBx_sdk_dualNewSDK.E_txtDirection.pNORMAL;
            //pheader.txtSpace = 0;
            //pheader.Valign = 2;
            //pheader.Halign = 2;


            IntPtr aa = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OnbonBx_sdk_dualNewSDK.EQareaHeader_G6)));
            Marshal.StructureToPtr(aheader, aa, false);
            IntPtr bb = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OnbonBx_sdk_dualNewSDK.EQpageHeader_G6)));
            Marshal.StructureToPtr(pheader, bb, false);
            //OnbonBx_sdk_dualNewSDK.EQpageHeader_G6 pheader1;
            //pheader1.PageStyle = 0x00;
            //pheader1.DisplayMode = 0x03;
            //pheader1.ClearMode = 0x01;
            //pheader1.Speed = 15;
            //pheader1.StayTime = 500;
            //pheader1.RepeatTime = 1;
            //pheader1.ValidLen = 0;
            //pheader1.CartoonFrameRate = 0x00;
            //pheader1.BackNotValidFlag = 0x00;
            //pheader1.arrMode = OnbonBx_sdk_dualNewSDK.E_arrMode.eSINGLELINE;
            //pheader1.fontSize = 18;
            //pheader1.color = (uint)0x01;
            //pheader1.fontBold = 0;
            //pheader1.fontItalic = 0;
            //pheader1.tdirection = OnbonBx_sdk_dualNewSDK.E_txtDirection.pNORMAL;
            //pheader1.txtSpace = 0;
            //pheader1.Valign = 2;
            //pheader1.Halign = 2;
            //IntPtr aa1 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OnbonBx_sdk_dualNewSDK.EQareaHeader_G6)));
            // Marshal.StructureToPtr(aheader1, aa1, false);
            // IntPtr bb1 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OnbonBx_sdk_dualNewSDK.EQpageHeader_G6)));
            // Marshal.StructureToPtr(pheader1, bb1, false);
            //删除单个动态区 zc
            //err = OnbonBx_sdk_dualNewSDK.dynamicArea_DelArea_6G(ip, 5005, 0xff);
            //Console.WriteLine("dynamicArea_DelArea_6G = " + err);
            //单区域文本 zc
            //err = OnbonBx_sdk_dualNewSDK.dynamicArea_AddAreaTxt_6G(ip, 5005, OnbonBx_sdk_dualNewSDK.E_ScreenColor_G56.eSCREEN_COLOR_SINGLE, 0, 0, 0,96
            // , 48, font, 12, str);
            //Console.WriteLine("dynamicArea_AddAreaTxt_6G = " + err);
            //单区域图片 zc
            //err = OnbonBx_sdk_dualNewSDK.dynamicArea_AddAreaPic_6G(ip, 5005, OnbonBx_sdk_dualNewSDK.E_ScreenColor_G56.eSCREEN_COLOR_FULLCOLOR, 0, 0, 0,
            //32, 32, bb, img);
            //Console.WriteLine("dynamicArea_AddAreaPic_6G = " + err);

            //更新动态区，可设置属性 zc
            err = OnbonBx_sdk_dualNewSDK.bxDual_dynamicArea_AddAreaTxtDetails_6G(ip, 5005, OnbonBx_sdk_dualNewSDK.E_ScreenColor_G56.eSCREEN_COLOR_SINGLE, 0,
                aa, bb, font, str);
            //Console.WriteLine("dynamicArea_AddAreaTxtDetails_6G = " + err);
            Common.ShowBlackBox.WriteLine("1 " + err);
            //关联节目 zc
            //ushort[] RelateProSerial = new ushort[1] { 0};
            //err = OnbonBx_sdk_dualNewSDK.dynamicArea_AddAreaTxtDetails_WithProgram_6G(ip, 5005, OnbonBx_sdk_dualNewSDK.E_ScreenColor_G56.eSCREEN_COLOR_FULLCOLOR, 0, aa, bb, font, str, 0, RelateProSerial);
            //Console.WriteLine("dynamicArea_AddAreaTxtDetails_WithProgram_6G = " + err);


            //OnbonBx_sdk_dualNewSDK.DynamicAreaParams[] Params = new OnbonBx_sdk_dualNewSDK.DynamicAreaParams[1];
            Params[0].uAreaId = 0;
            Params[0].oAreaHeader_G6 = aheader;
            Params[0].stPageHeader = pheader;
            Params[0].fontName = BytesToIntptr(font);
            Params[0].strAreaTxtContent = BytesToIntptr(str);
            // Params[1].uAreaId = 1;
            // Params[1].oAreaHeader_G6 = aheader1;
            //  Params[1].stPageHeader = pheader1;
            //  Params[1].fontName = BytesToIntptr(font);
            // Params[1].strAreaTxtContent = BytesToIntptr(img);

            // err = OnbonBx_sdk_dualNewSDK.dynamicAreaS_AddTxtDetails_6G(ip, 5005, OnbonBx_sdk_dualNewSDK.E_ScreenColor_G56.eSCREEN_COLOR_SINGLE, 1, Params);
            // Console.WriteLine("dynamicAreaS_AddTxtDetails_6G = " + err);
        }

        //byte[]转换为Intptr
        public static IntPtr BytesToIntptr(byte[] bytes)
        {
            int size = bytes.Length;
            IntPtr buffer = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.Copy(bytes, 0, buffer, size);
                return buffer;
            }
            finally
            {
                //Marshal.FreeHGlobal(buffer);
            }
        }
      


    }
}
